; BierIRC - DEvixl & Wesley
;
; 
; 2010 
; 
;  Hier alles opsommen wat je gedaan hebt, anders is het niet meer te overzien.
;
;
;


Global ConnectionID.l
Global NewList RecText.s()
Global nick.s, Server.s, Channel.s, Login.i

Global Dim brush(4) 

Enumeration 
  #C0Brush 
  #C1Brush 
  #C2Brush 
  #SelectedBrush 
EndEnumeration 

InitNetwork()

; Constanten van IRC netwerk
;----------- ERROR RESPONSES  --- DEvixl
#ERR_NOSUCHNICK = 401
#ERR_NOSUCHSERVER = 402
#ERR_NOSUCHCHANNEL = 403
#ERR_CANNOTSENDTOCHANNEL = 404
#ERR_TOOMANYCHANNELS = 405
#ERR_WASNOSUCHNICK = 406
#ERR_TOOMANYTARGETS = 407
#ERR_NOORIGIN = 409
#ERR_NORECIPIENT = 411
#ERR_NOTEXTTOSEND = 412
#ERR_NOTOPLEVEL = 413
#ERR_WILDTOPLEVEL = 414
#ERR_UNKNOWNCOMMAND = 421
#ERR_NOMOTD = 422
#ERR_NOADMININFO = 423
#ERR_FILEERROR = 424
#ERR_NONICKNAMEGIVEN = 431
#ERR_ERRONEUSNICKNAME = 432
#ERR_NICKNAMEINUSE = 433
#ERR_NICKCOLLISION = 436
#ERR_USERNOTINCHANNEL = 441
#ERR_NOTONCHANNEL = 442
#ERR_USERONCHANNEL = 443
#ERR_NOLOGIN = 444
#ERR_SUMMONDISABLED = 445
#ERR_USERDISABLED = 446
#ERR_NOTREGISTERED = 451
#ERR_NEEDMOREPARAMS = 461
#ERR_ALREADYREGISTERED = 462
#ERR_NOPERMFORHOST = 463
#ERR_PASSWDMISMATCH = 464
#ERR_YOUREBANNEDCREEP = 465
#ERR_KEYSET = 467
#ERR_CHANNELISFULL = 471
#ERR_UNKOWNMODE = 472
#ERR_INVITEONLYCHAN = 473
#ERR_BANNEDFROMCHAN = 474
#ERR_BADCHANNELKEY = 475
#ERR_NOPRIVILEGES = 481
#ERR_CHANOPRIVSNEEDED = 482
#ERR_CANTKILLSERVER = 483
#ERR_NOOPERHOST = 491
#ERR_UMODEUNKNOWNFLAG = 501
#ERR_USERSDONTMATCH = 502

;----------- COMMAND RESPONSES  --- Devixl
#RPL_NONE = 300
#RPL_USERHOST = 302
#RPL_ISON = 303
#RPL_AWAY = 301
#RPL_UNAWAY = 305
#RPL_NOWAWAY = 306
#RPL_WHOISUSER = 311
#RPL_WHOISSERVER = 312
#RPL_WHOISOPERATOR = 313
#RPL_WHOISIDLE = 317
#RPL_ENDOFWHOIS = 318
#RPL_WHOISCHANNELS = 319
#RPL_WHOWASUSER = 314
#RPL_ENDOFWHOWAS = 369
#RPL_LISTSTART = 321
#RPL_LIST = 322
#RPL_LISTEND = 323
#RPL_CHANNELMODEIS = 324
#RPL_NOTOPIC = 331
#RPL_TOPIC = 332
#RPL_INVITING = 341
#RPL_SUMMONING = 342
#RPL_VERSION = 351
#RPL_WHOREPLY =352
#RPL_ENDOFWHO = 315
#RPL_NAMREPLY = 353
#RPL_ENDOFNAMES = 366
#RPL_LINKS = 364
#RPL_ENDOFLINKS = 365
#RPL_BANLIST = 367
#RPL_ENDOFBANLIST = 368
#RPL_INFO = 371
#RPL_ENDOFINFO = 374
#RPL_MOTDSTART = 375
#RPL_MOTD = 372
#RPL_ENDOFMOTD = 376
#RPL_YOUREOPER = 381
#RPL_REHASHING = 382
#RPL_TIME = 391
#RPL_USERSSTART = 392
#RPL_USERS = 393
#RPL_ENDOFUSERS = 394
#RPL_NOUSERS = 395




IncludeFile "BierIRC_Functies.pb"


Channel.s = "#test"
Server.s = "irc.bierdopje.com"
Nick.s = InputRequester("Nickname", "Geef je nickname:", "BierChat_Test")


CreateMAinWindow()

IRCConnect(Server.s, 6667)
IRCLogin(Server.s, Nick.s, "")
IRCJoin(Channel.s, Server.s)

Repeat
   ;Delay(1)  ; Anders slikt hij de gehele CPU weg
   
   If NetworkClientEvent(ConnectionID) = 2 
     ParseIRCData()    
   EndIf
  
   If NetworkClientEvent(ConnectionID) = 2
    Line.s = IRCGetLine()  
   Else
     line.s = ""
   EndIf
   
  
    If Line <> ""
      CreateUserList() 
      
     If IRCGetFrom(Line) <> ""
      
      
      If UCase(IRCGetTo(Line)) <> UCase(Channel.s)
        ; PM gedeelte
        AddGadgetItem(0, -1, "["+FormatDate("%hh:%ii:%ss", Date())+ "] "+IRCGetFrom(Line)+" To "+IRCGetTo(Line)+ " : " +IRCGetText(Line))
      Else
        AddGadgetItem(0, -1,"["+FormatDate("%hh:%ii:%ss", Date())+ "] "+ IRCGetFrom(Line) +" : "+IRCGetText(Line))
      EndIf
      
     Else
    ; Standaard output, nog te parsen   <------------------------- Zie constanten
    ;AddGadgetItem(0, -1, Line)   
     EndIf
      
      ;Bij nieuw bericht naar beneden scrollen
      SetGadgetState(0, CountGadgetItems(0)-1)
    
    Else
    
    ; Pingen
      If Login = 1 And ElapsedMilliseconds()-LastPing > 14000
        Ping.f = IRCPing(Server.s, 5000)
        SetWindowTitle(0, "BierIRC  "+Channel+"  Ping: "+StrF(Ping, 2))
        LastPing = ElapsedMilliseconds()
      EndIf
    
   EndIf
  
  Event = WindowEvent()
  Select Event
    Case 0
      Delay(1)
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1
            If GetGadgetText(1) <> ""
             If Left(GetGadgetText(1), 1) = "/"
              AllParams.s = Right(GetGadgetText(1), Len(GetGadgetText(1))-FindString(GetGadgetText(1), " ", 0))
              Param1.s = StringField(GetGadgetText(1), 2, " ")
              Param2.s = Right(AllParams.s, Len(AllParams.s)-FindString(AllParams.s, " ", 1))
              Select LCase(StringField(GetGadgetText(1), 1, " "))
                Case "/msg"
                  IRCSendText(Param1, Param2)
                  ;Maken dat er een nieuwe tab onstaat voor PM chat..
                  AddGadgetItem(0, -1, "<"+Nick+" To "+Param1+"> "+Param2)
                Case "/join"
                  ;Nieuwe Tab maken voor het joinen
                  IRCJoin(Param1, Server)
                Default
                  IRCSend(Right(GetGadgetText(1), Len(GetGadgetText(1))-1))
                  AddGadgetItem(0, -1,"["+FormatDate("%hh:%ii:%ss", Date())+ "]" + Chr(10) +Nick+ Chr(10)+AllParams.s)
              EndSelect
            Else
              IRCSendText(Channel.s, GetGadgetText(1))
              AddGadgetItem(0, -1, "["+FormatDate("%hh:%ii:%ss", Date())+ "] " +Nick+ " : " +GetGadgetText(1))
            EndIf
          
            SetGadgetText(1, "")
            SetGadgetState(0, CountGadgetItems(0)-1)
            
            EndIf
      EndSelect
      
    Case #PB_Event_SizeWindow
      ResizeWin()
    Case #PB_Event_Gadget
      Select EventGadget()
;         Case 2
;           If EventType() = #PB_EventType_LeftDoubleClick
;             Msg.s = GetGadgetItemText(2, GetGadgetState(2), 0)
;             If Left(Msg, 1) = "@"
;               Msg = Right(Msg, Len(Msg)-1)
;             EndIf
;             SetGadgetText(1, GetGadgetText(1)+"/msg "+Msg+" ")
;             DisableGadget(1,0)
;             ;ActivateGadget(1)
;           EndIf       
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow

IRCDisconnect("De test van de C client BierChat is afgesloten :)")
 

End

; IDE Options = PureBasic 4.51 (Windows - x86)
; CursorPosition = 122
; FirstLine = 110
; EnableXP
; Executable = BierChat.exe